package org.lisen.patterndemo.singleton;

/**
 * 单例模式，饥饿加载
 */
public class SingletonDemo01 {

    //1. 需要有一个私有的构造函数，防止该类通过new的方式创建实例
    private SingletonDemo01(){}

    //2. 饥饿模式，首先生成一个实例
    private static final SingletonDemo01 instance = new SingletonDemo01();

    //3. 静态方法，用于获取已经生成的实例
    public static SingletonDemo01 getInstance() {
        return instance;
    }

    public String hello(String name) {
        return "hello " + name;
    }

    /*
     * 测试多线程下单例模式是否安全
     * @param args
     */
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                System.out.println(SingletonDemo01.getInstance().hashCode());
            }).start();
        }
    }

}
